package com.bestcem.xm.ticket.entity.mongo;

import com.bestcem.xm.ticket.constant.ColumnConstant;
import com.bestcem.xm.ticket.enums.TicketOperationTypeEnum;
import com.bestcem.xm.ticket.enums.TicketStatusEnum;
import com.bestcem.xm.ticket.enums.TicketTypeEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 工单记录表
 * 当一个问卷下某份答卷触发规则时, 自动生成一份和答卷对应的工单记录
 * 一个工单规则配置对应多条工单记录
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/10 16:13
 */
@ToString(callSuper = true)
@Getter
@Setter
public class Ticket extends BaseEntity {

    private static final long serialVersionUID = -7253778583948098509L;

    /**
     * 工单名称，手动新建工单才会存在(2.3版本新增)
     */
    @JsonProperty(value = ColumnConstant.Ticket.NAME)
    private String name;

    /**
     * 系统id
     */
    @JsonProperty(value = ColumnConstant.Ticket.SYS_ID)
    private String sysId;

    /**
     * 公司id
     */
    @JsonProperty(value = ColumnConstant.Ticket.ORG_ID)
    private String orgId;
    /**
     * ticketNo 最大长度 32,格式：yyyymmdd00001
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_NO)
    private String ticketNo;


    /**
     * 问卷id
     */
    @JsonProperty(value = ColumnConstant.Ticket.SURVEY_ID)
    private String surveyId;

    /**
     * 问卷标题
     */
    @JsonProperty(value = ColumnConstant.Ticket.SURVEY_TITLE)
    private String surveyTitle;

    /**
     * 当前跟进的角色id
     */
    @JsonProperty(value = ColumnConstant.Ticket.FOLLOW_ROLE_ID)
    private String followRoleId;

    /**
     * 当前跟进的用户ids
     */
    @JsonProperty(value = ColumnConstant.Ticket.FOLLOW_USER_IDS)
    private List<String> followUserIds;

    /**
     * 实际审核的用户
     */
    @JsonProperty(value = ColumnConstant.Ticket.AUDIT_USER_IDS)
    private List<String> auditUserIds;

    /**
     * 当前审核的角色id
     */

    @JsonProperty(value = ColumnConstant.Ticket.AUDIT_ROLE_ID)
    private String auditRoleId;

    /**
     * 第几跟进人
     */
    @JsonProperty(value = ColumnConstant.Ticket.CURRENT_FOLLOW_NODE)
    private Integer currentFollowNode;

    /**
     * followNodeCount
     */
    @JsonProperty(value = ColumnConstant.Ticket.FOLLOW_NODE_COUNT)
    private Integer followNodeCount;

    /**
     * 当前审核节点
     */
    @JsonProperty(value = ColumnConstant.Ticket.CURRENT_AUDIT_NODE)
    private Integer currentAuditNode;

    /**
     * auditNodeCount
     */
    @JsonProperty(value = ColumnConstant.Ticket.AUDIT_NODE_COUNT)
    private Integer auditNodeCount;

    /**
     * region
     */
    @JsonProperty(value = ColumnConstant.Ticket.REGION)
    private String region;

    /**
     * regionName
     */
    @JsonProperty(value = ColumnConstant.Ticket.REGION_NAME)
    private String regionName;

    /**
     * alertId
     */
    @JsonProperty(value = ColumnConstant.Ticket.ALERT_ID)
    private String alertId;

    /**
     * alertRuleIds ObjectId 数组
     */
    @JsonProperty(value = ColumnConstant.Ticket.ALERT_RULE_IDS)
    private List<String> alertRuleIds;

    /**
     * resId
     */
    @JsonProperty(value = ColumnConstant.Ticket.RES_ID)
    private Long resId;

    /**
     * contact
     * <p>
     * 这个字段我不知道怎么写, python里面是 dict 类型, 但是库里没有数据, 所以我不知道到底是个什么样子
     * 沟通记录: @赵明: 这个是历史字段了，我接手后，也没用过
     */
    @JsonProperty(value = ColumnConstant.Ticket.CONTACT)
    private Contact contact;

    /**
     * status
     *
     * @see TicketStatusEnum
     */
    @JsonProperty(value = ColumnConstant.Ticket.STATUS)
    private Integer status;

    /**
     * 是否跟进超时
     */
    @JsonProperty(value = ColumnConstant.Ticket.HAS_FOLLOW_OVERDUE)
    private Boolean hasFollowOverdue;

    /**
     * 跟进超时时间
     */
    @JsonProperty(value = ColumnConstant.Ticket.FOLLOW_DEADLINE)
    private Date followDeadline;

    /**
     * 是否审核超时
     */
    @JsonProperty(value = ColumnConstant.Ticket.HAS_AUDIT_OVERDUE)
    private Boolean hasAuditOverdue;

    /**
     * 审核超时时间
     */
    @JsonProperty(value = ColumnConstant.Ticket.AUDIT_DEADLINE)
    private Date auditDeadline;

    /**
     * 工单超时时间，与第一第二节点审核和跟进超时时间保持一致
     * 例如：当前审核节点是1，状态时跟进中，那么工单超时时间就是第一节点跟进的超时时间
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_DEADLINE)
    private Date ticketDeadline;

    /**
     * ticketConfigId
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_CONFIG_ID)
    private String ticketConfigId;

    /**
     * ticketFlow
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_FLOW)
    private TicketFlow ticketFlow;

    /**
     * ticketTemplate
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_TEMPLATE)
    private TicketTemplate ticketTemplate;

    /**
     * ticketTemplateV2
     */
    private TicketTemplateV2 ticketTemplateV2;

    /**
     * ticketRecordIds ObjectId 数组
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_RECORD_IDS)
    private List<String> ticketRecordIds;

    /**
     * closedDT
     */
    @JsonProperty(value = ColumnConstant.Ticket.CLOSED_D_T)
    private Date closedDate;

    /**
     * 当前工单要进行的流程 ObjectId 数组
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_FLOW_LIST)
    private List<String> ticketFlowList;

    /**
     * 工单处理完成时间
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_FINISH_TIME)
    private Date ticketFinishTime;

    /**
     * 工单处理失败时间
     */
    @JsonProperty(value = ColumnConstant.Ticket.TICKET_FAILED_TIME)
    private Date ticketFailedTime;

    /**
     * 当工单处理人是按照用户跟进时,该字段有值为当前跟进人id
     */
    @JsonProperty(value = ColumnConstant.Ticket.CUR_FOLLOW_USER_ID)
    private List<String> curFollowUserId;

    /**
     * 触发满足哪条规则的record_id
     */
    @JsonProperty(value = ColumnConstant.Ticket.ALERT_RECORD_ID)
    private String alertRecordId;

    /**
     * 工单详情 显示内容
     */
    @JsonProperty(value = ColumnConstant.Ticket.TEMPLATE_DETAIL_FIELDS)
    private List<TemplateDetailField> templateDetailFields;

    /**
     * 是否至少需要一次提交记录
     */
    @JsonProperty(value = ColumnConstant.Ticket.NEED_RECORD)
    private Boolean needRecord;

    /**
     * 工单类型: 工单 | 预警
     *
     * @see TicketTypeEnum
     */
    @JsonProperty(value = ColumnConstant.Ticket.TTYPE)
    private Integer type;

    /**
     * 是否已有跟进记录
     */
    @JsonProperty(value = ColumnConstant.Ticket.HAS_COMMIT)
    private Boolean hasCommit;

    /**
     * member_id (工单2.2P3加入)
     */
    @JsonProperty(value = ColumnConstant.Ticket.MEMBER_ID)
    private String memberId;

    /**
     * 详情模板id  (工单2.3加入)
     */
    @JsonProperty(value = ColumnConstant.Ticket.DETAIL_TEMPLATE_ID)
    private String detailTemplateId;

    /**
     * 处理模板id(工单2.3加入)
     */
    @JsonProperty(value = ColumnConstant.Ticket.HANDLE_TEMPLATE_ID)
    private String handleTemplateId;

    /**
     * 1-系统创建，2-手动创建（2.3版本新增）
     *
     * @see TicketOperationTypeEnum
     */
    @JsonProperty(value = ColumnConstant.Ticket.OPERATION_TYPE)
    private Integer operationType;

    /**
     * 创建人名字
     */
    @JsonProperty(value = ColumnConstant.Ticket.CREATED_NAME)
    private String createdName;

    /**
     * 是否sample
     */
    @JsonProperty(value = ColumnConstant.Ticket.SAMPLE)
    private Boolean sample;


    /**
     * 工单满足的触发条件的名称
     * 2022-02-18 行动新增条件名称需求新增
     */
    @JsonProperty(value = ColumnConstant.Ticket.TRIGGER_NAMES)
    private LinkedList<String> triggerNames;

    /**
     * 工单详情 显示内容
     *
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @version v1.0
     * @date 2021/8/10 17:50
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class TemplateDetailField implements Serializable {
        private static final long serialVersionUID = 2808726620840986656L;

        /**
         * content
         */
        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.CONTENT)
        private String content;

        /**
         * sourceId
         */
        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.SOURCE_ID)
        private String sourceId;

        /**
         * originText
         */
        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.ORIGIN_TEXT)
        private String originText;

        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.VALUE)
        private SurveyAnswer value;

        /**
         * 选项类型
         * text--1单行文本； multiLine--2 多行文本； select---3单选； cascadeSelect--4 级联选择
         */
        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.TYPE)
        private Integer type;

        /**
         * 是否必填
         */
        @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.REQUIRED)
        private Boolean required;

        @Data
        public static class SurveyAnswer implements Serializable {
            private static final long serialVersionUID = -7182492284143665603L;

            @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.SurveyAnswer.ANSWER)
            private Object answer;

            @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.SurveyAnswer.CUSTOM_Q_TYPE)
            private String customQType;


            @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.SurveyAnswer.Q_TYPE)
            private String qtype;

            @JsonProperty(value = ColumnConstant.Ticket.TemplateDetailField.SurveyAnswer.C_ID)
            private String cId;


        }
    }

    /**
     * 联系人信息
     */
    @Data
    public static class Contact implements Serializable {
        private static final long serialVersionUID = 3751020835484697267L;
        /**
         * 手机号
         */
        @JsonProperty(value = ColumnConstant.Ticket.Contact.PHONE)
        private String phone;

        /**
         * 邮箱
         */
        @JsonProperty(value = ColumnConstant.Ticket.Contact.EMAIL)
        private String email;
    }
}
